using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace eBirdAttend
{
    public partial class Branch : UserControl
    {
        public string strCon = Properties.Settings.Default["ConString"].ToString();
        SqlConnection sqlCon = new SqlConnection();
        SqlCommand sqlCmd = new SqlCommand();
        String strBtnCaption = "";

        public Branch()
        {
            InitializeComponent();
            sqlCon.ConnectionString = strCon;
        }

        // Function name   : btnAdd_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            ClearControls();
           
            //to set the branch id
            sqlCmd.CommandText = "select max(brbranchid) from branch";
            sqlCmd.Connection = sqlCon;
            sqlCon.Open();
            SqlDataReader sqlReader;
            sqlReader = sqlCmd.ExecuteReader();
            if (sqlReader.Read())
            {
                if (sqlReader[0].ToString() == "")
                {
                }
                else
                {
                    txtBrId.Text = Convert.ToString(Convert.ToInt32(sqlReader[0].ToString()) + 1);
                }
            }
            else
            {
                txtBrId.Text = "1";
            }

            if (txtBrId.Text == "")
            {
                txtBrId.Text = "1";
            }
          
            sqlCon.Close();
          
            groupBox1.Visible = true;
            strBtnCaption = "Add";
            groupBox1.Text = "ADD A BRANCH"; 
            txtBrName.Focus();
        }


        // Function name   : btnEdit_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnEdit_Click(object sender, EventArgs e)
        {
           ClearControls();
            if (brachview.SelectedRows.Count == 0)
            { MessageBox.Show("Please select a row for edit"); return; }

            if ((brachview.RowCount - 1) == brachview.SelectedRows[0].Index)
            {
                MessageBox.Show("No content to edit");
                groupBox1.Visible = false;             
                ClearControls();
                return;
            }
            
            groupBox1.Visible = true;
            strBtnCaption = "Edit";
            groupBox1.Text = "EDIT A BRANCH";
            txtBrName.Focus();
          
            //MessageBox.Show(dataGridView1.SelectedRows[0].Cells[1].Value.ToString());
          
            txtBrId.Text = brachview.SelectedRows[0].Cells[1].Value.ToString();
            txtBrName.Text = brachview.SelectedRows[0].Cells[2].Value.ToString();
            txtBrAdd.Text = brachview.SelectedRows[0].Cells[3].Value.ToString();
            txtBrCity.Text = brachview.SelectedRows[0].Cells[4].Value.ToString();
            txtBrPhone.Text = brachview.SelectedRows[0].Cells[5].Value.ToString();
            txtBrFax.Text = brachview.SelectedRows[0].Cells[6].Value.ToString();
            txtBrIncharge.Text = brachview.SelectedRows[0].Cells[7].Value.ToString();          
             
        }

        // Function name   : btnDelete_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (brachview.SelectedRows.Count == 0)
                {
                    sqlCon.Close();
                    MessageBox.Show("Please select a row to delete"); return;
                }

                DialogResult _dlg = MessageBox.Show("Are you sure you want to delete?", "Warning", MessageBoxButtons.YesNo);
                if (_dlg == DialogResult.No)
                {
                    return;
                }
                else
                {

                    string strbrID = brachview.SelectedRows[0].Cells[1].Value.ToString();
                    while (brachview.Rows.Count > 1)
                        brachview.Rows.RemoveAt(0);
                    sqlCmd.CommandText = "delete from Branch where BrBranchId ='" + strbrID + "'";
                    sqlCmd.Connection = sqlCon;
                    sqlCon.Open();
                    sqlCmd.ExecuteNonQuery();
                    sqlCon.Close();
                    branchTableAdapter.Fill(branchDataSet1.Branch);

                    MessageBox.Show("Successfuly Deleted");
                }
            }
            catch (Exception ex)
            {
            }
        }


        // Function name   : btnSave_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateControls())
                {
                    //save the new branch details
                    if (strBtnCaption == "Add")
                    {
                        string query = "INSERT INTO Branch(BrCmpId, BrName, BrAddress, BrCity, BrPhone, BrFax, BrInCharge) " +
                                            " VALUES     (1,'" + txtBrName.Text + "','" + txtBrAdd.Text + "','" + txtBrCity.Text + "','" + txtBrPhone.Text + "','" + txtBrFax.Text + "','" + txtBrIncharge.Text+"')";
                        while (brachview.Rows.Count > 1)
                            brachview.Rows.RemoveAt(0);
                        sqlCmd.CommandText = query;
                        sqlCmd.Connection = sqlCon;
                        sqlCon.Open();
                        sqlCmd.ExecuteNonQuery();
                      
                        branchTableAdapter.Fill(branchDataSet1.Branch);

                        MessageBox.Show("Successfuly Inserted");
                        groupBox1.Visible = false;
                    }
                    else
                    {
                        //update the branch details
                        while (brachview.Rows.Count > 1)
                            brachview.Rows.RemoveAt(0);

                        sqlCmd.CommandText = "update  Branch set BrCmpId='" + "1" + "',BrName='" + txtBrName.Text + "',BrAddress='" + txtBrAdd.Text + "',BrCity='" + txtBrCity.Text + "', BrPhone='" + txtBrPhone.Text + "',BrFax='" + txtBrFax.Text + "' ,BrInCharge='" + txtBrIncharge.Text + "' where BrBranchId='" + txtBrId.Text + "'";
                        sqlCmd.Connection = sqlCon;
                        sqlCon.Open();
                        sqlCmd.ExecuteNonQuery();
                       
                        branchTableAdapter.Fill(branchDataSet1.Branch);
                        MessageBox.Show("Successfuly Updated");
                        groupBox1.Visible = false;
                    }
                }
            }
            catch (Exception e1)
            {
                sqlCon.Close();
                ClearControls();
            }
            finally
            {
                if(sqlCon.State == ConnectionState.Open)
                sqlCon.Close();
                //groupBox1.Visible = false;
                //ClearControls();
            }
        }

        private void Branch_Load(object sender, EventArgs e)
        {
            branchTableAdapter.Fill(branchDataSet1.Branch);
            brachview.Columns[1].Visible = false;
            brachview.Columns[0].Visible = false;
        }

        // Function name   : btnCancel_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnCancel_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
            btnEdit.Visible = true;
            ClearControls();
        }

        // Function name   : ClearControls
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/18/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        
        public void ClearControls()
        {
            txtBrId.Text = "";
            txtBrName.Text = "";
            txtBrAdd.Text = "";
            txtBrCity.Text = "";           
            txtBrPhone.Text = "";
            txtBrFax.Text = "";
            txtBrIncharge.Text = "";
            groupBox1.Visible = false;
        }

        // Function name   : ValidateControls
        // Description     : 
        // Return type     : bool 
        // Author          : Priya Purushothaman
        // Date            : 10/18/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        
        public bool ValidateControls()
        {
            if (txtBrName.Text == "")
            {
                MessageBox.Show("Enter the branch name");
                txtBrName.Focus();
                return false;
            }
            else if (txtBrCity.Text == "")
            {
                MessageBox.Show("Enter the city");
                txtBrCity.Focus();
                return false;
            }
            else if (txtBrPhone.Text == "")
            {
                MessageBox.Show("Enter the phone number");
                txtBrPhone.Focus();
                return false;
            }
            else
            {
                if (txtBrPhone.Text != "")
                {
                    //validate the number textboxes
                    try
                    {
                        //validate the phone number
                        //txtBrPhone.Text = Convert.ToString(Convert.ToInt32(txtBrPhone.Text));
                        if (txtBrFax.Text != "")
                        {
                            try
                            {
                                //validate the fax number
                                txtBrFax.Text = Convert.ToString(Convert.ToInt32(txtBrFax.Text));
                                return true;
                            }
                            catch (Exception e1)
                            {
                                MessageBox.Show("Enter a valid Fax No");
                                txtBrFax.Focus();
                                return false;
                            }
                        }
                    }
                    catch (Exception e1)
                    {
                        MessageBox.Show("Enter a valid Phone No");
                        txtBrPhone.Focus();
                        return false;
                    }
                }
            }
            return true;
        }

        // Function name   : btnClose_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/19/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
